package com.jiezhuo.controller.jss.openApi;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jiezhuo.entity.cmeduSchool.JwXqStudent;
import com.jiezhuo.entity.cmeduSchool.TJzg;
import com.jiezhuo.entity.cmeduSchool.TStuJtcy;
import com.jiezhuo.entity.cmeduSchool.XxdwXq;
import com.jiezhuo.entity.jssgl.JssStuJbqk;
import com.jiezhuo.service.IJwXqNjXjService;
import com.jiezhuo.service.IJwXqStudentService;
import com.jiezhuo.service.cmeduSchool.ITStuJtcyService;
import com.jiezhuo.service.cmeduSchool.StuService;
import com.jiezhuo.service.cmeduSchool.XxdwService;
import com.jiezhuo.service.cmeduSchool.XxdwXqService;
import com.jiezhuo.service.jss.JssCommonService;
import com.jiezhuo.service.jss.JssStuGagzjlService;
import com.jiezhuo.service.jss.JssStuJbqkService;
import com.jiezhuo.service.jss.JssXtUserlistService;
import com.jiezhuo.service.jzg.TJzgService;
import com.jiezhuo.utils.R;
import com.jiezhuo.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RequiredArgsConstructor
@RestController
@Api(tags = "对外api")
@RequestMapping("/openApi")
@Transactional
public class OpenApiController {

    @Autowired
    JssStuGagzjlService jssStuGagzjlService;

    @Autowired
    IJwXqStudentService jwXqStudentService;

    @Autowired
    TJzgService tJzgService;

    @Autowired
    XxdwXqService xxdwXqService;

    @Autowired
    XxdwService xxdwService;

    @Autowired
    JssStuJbqkService jssStuJbqkService;

    @Autowired
    ITStuJtcyService itStuJtcyService;

    @Autowired
    JssCommonService jssCommonService;

    @Autowired
    JssXtUserlistService jssXtUserlistService;

    @Autowired
    private IJwXqNjXjService jwXqNjXjService;

    @Autowired
    StuService stuService;

    @Autowired
    private IJwXqStudentService iJwXqStudentService;

    @GetMapping("getAllSchoolInfo")
    @ApiOperation("获取校区信息")
    public R getAllSchoolInfo(String isJss) {
        QueryWrapper<XxdwXq> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isStringExist(isJss)) {
            queryWrapper.eq("SFJSZGL", "1");
            queryWrapper.eq("DQZT", "1");
            queryWrapper.orderByAsc("XQJC");
        }
        List<XxdwXq> xxdwXqList = xxdwXqService.list(queryWrapper);
        return R.ok().data("data", xxdwXqList);
    }

    @GetMapping("getStudentInfo")
    @ApiOperation("获取学生信息")
    public R getStudentInfo(@RequestParam("xq") String xq, @RequestParam("stuId") String stuId) {
        HashMap<String, Object> map = new HashMap<>();
        List<Map<String, Object>> headerLoadData = stuService.getJssSqEditLoad(xq, stuId);
        // 身份证脱敏
        headerLoadData = headerLoadData.stream().peek(x -> {
            if (StringUtils.isStringExist(x.get("SFZJH") != null ? x.get("SFZJH").toString() : "")) {
                x.put("SFZJH", StringUtils.maskID(x.get("SFZJH").toString()));
            }
        }).collect(Collectors.toList());
        // 家庭情况
        QueryWrapper<TStuJtcy> queryWrapperJtcy = new QueryWrapper<>();
        queryWrapperJtcy.eq("stu_id", stuId);
        List<TStuJtcy> tStuJtcyList = itStuJtcyService.list(queryWrapperJtcy);

        map.put("info", headerLoadData);
        map.put("jbqkList", tStuJtcyList);

        return R.ok().data("data", map);
    }

    @GetMapping("/getSchoolNj")
    @ApiOperation("获取学校年级")
    public R getSchoolNj(@RequestParam("newXqdm") String newXqdm, @RequestParam("xq") String xq) {
        // 参数
        JwXqStudent jwXqStudent = new JwXqStudent();
        jwXqStudent.setXq(xq);
        jwXqStudent.setNewXqdm(newXqdm);

        List<Map<String, Object>> jssSqSaveLoadNj = iJwXqStudentService.getJssSqSaveLoadNj(jwXqStudent);
        return R.ok().data("data", jssSqSaveLoadNj);
    }

    @GetMapping("/getSchoolBj")
    @ApiOperation("获取学校班级")
    public R getSchoolBj(@RequestParam("newXqdm") String newXqdm, @RequestParam("xq") String xq, @RequestParam("nj") String nj) {
        JwXqStudent jwXqStudent = new JwXqStudent();
        jwXqStudent.setNj(nj);
        jwXqStudent.setXq(xq);
        jwXqStudent.setNewXqdm(newXqdm);
        List<Map<String, Object>> jssSqSaveLoadbj = iJwXqStudentService.getJssSqSaveLoadbj(jwXqStudent);
        jssSqSaveLoadbj = jssSqSaveLoadbj.stream().filter(x -> StringUtils.isStringExist(x.get("bj") != null ? x.get("bj").toString() : "")).collect(Collectors.toList());
        return R.ok().data("data", jssSqSaveLoadbj);
    }

    @GetMapping("/getBjStudent")
    @ApiOperation("获取班级学生")
    public R getBjStudent(@RequestParam("newXqdm") String newXqdm, @RequestParam("xq") String xq, @RequestParam("nj") String nj, @RequestParam("bj") String bj) {
        HashMap<String, Object> reMap = new HashMap<>();
        // 参数
        JwXqStudent jwXqStudent = new JwXqStudent();
        jwXqStudent.setNewXqdm(newXqdm);
        jwXqStudent.setXq(xq);
        jwXqStudent.setNj(nj);
        jwXqStudent.setBj(bj);

        // 参数到此结束
        List<Map<String, Object>> list = iJwXqStudentService.getJssSqSaveLoad(jwXqStudent);
        reMap.put("list", list);
        return R.ok().data("data", reMap);
    }

    @GetMapping("/getTeacherByXqdm")
    @ApiOperation("获取学校老师")
    public R getTeacherByXqdm(@RequestParam("newXqdm") String newXqdm) {
        QueryWrapper<TJzg> teacherQuery = new QueryWrapper<>();
        teacherQuery.eq("GZ_XQDM", newXqdm);
        List<TJzg> teacherList = tJzgService.list(teacherQuery);

        teacherList = teacherList.stream().peek(x -> {
            // 身份证脱敏
            if (StringUtils.isStringExist(x.getSfzjh() != null ? x.getSfzjh() : "")) {
                x.setSfzjh(StringUtils.maskID(x.getSfzjh()));
            }
            // 手机号码脱敏
            if (StringUtils.isStringExist(x.getLxdh() != null ? x.getLxdh() : "")) {
                x.setLxdh(StringUtils.maskPhoneNumber(x.getLxdh()));
            }
        }).collect(Collectors.toList());

        return R.ok().data("data", teacherList);
    }
}
